<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>两个数组的交集 II - 算法详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
        }
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .code-dots {
            display: flex;
            gap: 8px;
        }
        .dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        .dot-red { background: #ff5f56; }
        .dot-yellow { background: #ffbd2e; }
        .dot-green { background: #27c93f; }
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            color: #d4d4d4;
            line-height: 1.6;
        }
        .keyword { color: #c586c0; }
        .function { color: #dcdcaa; }
        .string { color: #ce9178; }
        .number { color: #b5cea8; }
        .comment { color: #6a9955; }
        .operator { color: #d4d4d4; }
        .builtin { color: #4ec9b0; }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .complexity-badge {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 0.5rem 1rem;
            border-radius: 9999px;
            font-size: 0.875rem;
            font-weight: 500;
            display: inline-block;
            margin: 0.25rem;
        }
        .section-divider {
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            margin: 2rem auto;
            border-radius: 2px;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-4xl mx-auto text-center">
            <div class="mb-6">
                <i class="fas fa-code text-5xl opacity-80"></i>
            </div>
            <h1 class="text-5xl font-bold mb-4" style="font-family: 'Noto Serif SC', serif;">
                两个数组的交集 II
            </h1>
            <p class="text-xl opacity-90 leading-relaxed">
                探索高效算法，寻找数组间的共同元素
            </p>
            <div class="mt-8 flex justify-center gap-4">
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-hashtag mr-2"></i>哈希表
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-arrows-alt-h mr-2"></i>双指针
                </span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Problem Description -->
        <section class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <h2 class="text-3xl font-bold mb-6 text-gray-800" style="font-family: 'Noto Serif SC', serif;">
                <i class="fas fa-puzzle-piece text-purple-600 mr-3"></i>题目描述
            </h2>
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">给</span>定两个数组，编写一个函数来计算它们的交集。输出结果中每个元素出现的次数应与它在两个数组中出现次数的最小值一致。
            </p>
            <div class="mt-6 bg-gray-50 rounded-xl p-6">
                <h3 class="font-semibold text-gray-800 mb-3">
                    <i class="fas fa-lightbulb text-yellow-500 mr-2"></i>示例
                </h3>
                <div class="grid md:grid-cols-2 gap-4">
                    <div class="bg-white rounded-lg p-4 border border-gray-200">
                        <p class="text-sm text-gray-600 mb-1">输入：</p>
                        <code class="text-purple-600">nums1 = [1, 2, 2, 1]</code><br>
                        <code class="text-purple-600">nums2 = [2, 2]</code>
                    </div>
                    <div class="bg-white rounded-lg p-4 border border-gray-200">
                        <p class="text-sm text-gray-600 mb-1">输出：</p>
                        <code class="text-green-600">[2, 2]</code>
                        <p class="text-sm text-gray-500 mt-2">因为 2 在两个数组中都出现了两次</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Algorithm Visualization -->
        <section class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <h2 class="text-3xl font-bold mb-6 text-gray-800" style="font-family: 'Noto Serif SC', serif;">
                <i class="fas fa-sitemap text-purple-600 mr-3"></i>算法可视化
            </h2>
            <div class="mermaid">
                graph TD
                    A[两个数组的交集 II] --> B[哈希表方法]
                    A --> C[双指针方法]
                    B --> D[统计频率]
                    B --> E[遍历匹配]
                    C --> F[数组排序]
                    C --> G[指针比较]
                    D --> H[O(n+m) 时间]
                    E --> I[O(min(n,m)) 空间]
                    F --> J[O(nlogn + mlogm) 时间]
                    G --> K[O(1) 空间]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style H fill:#27c93f,stroke:#fff,stroke-width:2px,color:#fff
                    style I fill:#27c93f,stroke:#fff,stroke-width:2px,color:#fff
                    style J fill:#ffbd2e,stroke:#fff,stroke-width:2px,color:#fff
                    style K fill:#27c93f,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </section>

        <!-- Solution Approaches -->
        <section class="grid md:grid-cols-2 gap-8 mb-8">
            <!-- Hash Table Approach -->
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="flex items-center mb-6">
                    <div class="w-12 h-12 bg-purple-100 rounded-full flex items-center justify-center mr-4">
                        <i class="fas fa-hashtag text-purple-600 text-xl"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800">哈希表方法</h3>
                </div>
                <p class="text-gray-700 mb-4 leading-relaxed">
                    统计第一个数组中各元素的频率，然后遍历第二个数组，检查元素是否存在于哈希表中并减少频率。
                </p>
                <div class="section-divider"></div>
                <div class="space-y-3">
                    <div class="flex items-center">
                        <i class="fas fa-clock text-green-500 mr-3"></i>
                        <span class="text-gray-700">时间复杂度：</span>
                        <span class="complexity-badge ml-2">O(n + m)</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-memory text-blue-500 mr-3"></i>
                        <span class="text-gray-700">空间复杂度：</span>
                        <span class="complexity-badge ml-2">O(min(n, m))</span>
                    </div>
                </div>
            </div>

            <!-- Two Pointers Approach -->
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="flex items-center mb-6">
                    <div class="w-12 h-12 bg-purple-100 rounded-full flex items-center justify-center mr-4">
                        <i class="fas fa-arrows-alt-h text-purple-600 text-xl"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800">双指针方法</h3>
                </div>
                <p class="text-gray-700 mb-4 leading-relaxed">
                    对两个数组排序后，使用双指针遍历，比较元素并记录交集。
                </p>
                <div class="section-divider"></div>
                <div class="space-y-3">
                    <div class="flex items-center">
                        <i class="fas fa-clock text-yellow-500 mr-3"></i>
                        <span class="text-gray-700">时间复杂度：</span>
                        <span class="complexity-badge ml-2">O(n log n + m log m)</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-memory text-green-500 mr-3"></i>
                        <span class="text-gray-700">空间复杂度：</span>
                        <span class="complexity-badge ml-2">O(1)</span>
                    </div>
                </div>
            </div>
        </section>

        <!-- Code Implementation -->
        <section class="bg-white rounded-2xl shadow-xl overflow-hidden mb-8 card-hover">
            <div class="bg-gradient-to-r from-purple-600 to-purple-800 text-white p-6">
                <h2 class="text-3xl font-bold flex items-center">
                    <i class="fas fa-code mr-3"></i>示例代码
                    <span class="ml-4 text-lg font-normal opacity-90">哈希表方法</span>
                </h2>
            </div>
            <div class="code-block">
                <div class="code-header">
                    <div class="code-dots">
                        <div class="dot dot-red"></div>
                        <div class="dot dot-yellow"></div>
                        <div class="dot dot-green"></div>
                    </div>
                    <span class="text-gray-400 text-sm">Python</span>
                </div>
                <pre><code><span class="keyword">def</span> <span class="function">intersect</span>(nums1, nums2):
    <span class="keyword">from</span> <span class="builtin">collections</span> <span class="keyword">import</span> <span class="builtin">Counter</span>
    counter = <span class="builtin">Counter</span>(nums1)
    result = []
    <span class="keyword">for</span> num <span class="keyword">in</span> nums2:
        <span class="keyword">if</span> counter[num] <span class="operator">&gt;</span> <span class="number">0</span>:
            result.append(num)
            counter[num] <span class="operator">-=</span> <span class="number">1</span>
    <span class="keyword">return</span> result</code></pre>
            </div>
        </section>

        <!-- Key Insights -->
        <section class="bg-gradient-to-br from-purple-50 to-pink-50 rounded-2xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 text-gray-800" style="font-family: 'Noto Serif SC', serif;">
                <i class="fas fa-brain text-purple-600 mr-3"></i>核心洞察
            </h2>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white rounded-xl p-6 shadow-lg card-hover">
                    <div